<?php
namespace app\common\model;
use think\Model;
use think\Db;
/**
 * 试题
 */
class Exam extends Model
{
    
    protected $append = [
        'type_str',
        'topic_str',
        'knowledge_name',
        'cate_name',
        'item_str'
    ];

    /*protected static function init()
    {
        Subject::event('before_delete', function ($subject) {
           $count =  Db::name('knowledge')->where('subject_id',$subject['id'])->count();
           if($count >0){
               return false;
           }
        });
    }*/
    public function setTopicAttr($value,$data){
        if(!empty($value) && $data['cate'] == 1){
            return str_replace(['( )','（）','（ ）'],'()',$value);
        }
        return $value;
    }
    public function getTopicStrAttr($value,$data){
        if(!empty($data['topic']) && $data['cate'] == 1){
            $str = str_replace(['()','( )','（）','（ ）'],'<div class=\'layui-inline\'><input type=\'text\' name=\'text[]\' value=\'\' class=\'layui-input\'> </div>',$data['topic']);
        }else{
            $str = $data['topic'];
        }
        return $str;
    }
    public function getKnowledgeNameAttr($value,$data){
        $knowledge = Db::name('knowledge')->where('id',$data['knowledge_id'])->find();
        if($knowledge){
            $subject = Db::name('Subject')->where('id',$knowledge['subject_id'])->value('name');
            return $subject.'-'.$knowledge['title'];
        }
        return '';
    }
    public function getTypeStrAttr($value,$data){
        return Db::name('type')->where('id',$data['type_id'])->value('name');
    }
    public function getCateNameAttr($value,$data){
        switch ($data['cate']) {
            case '1':
                return '填空题';
                break;
            case '2':
                return '单选题';
                break;
            case '3':
                return '多选题';
                break;
            case '4':
                return '简答题';
                break;
            case '5':
                return '计算题';
                break;
            default:
                return '';
                break;
        }
    }
    public function getItemStrAttr($value,$data){
        if(empty($data['item'])){
            return '';
        }
        $itemArr = $this->getItemAttr($data['item'],$data);
        return implode("\r\n", array_column($itemArr,'item'));
    }
    public function getItemAttr($value,$data){
        if(empty($value)){
            return [];
        }else{
            $itemArr = json_decode($value,true);
            if($data['cate'] == 1){
                foreach($itemArr as $key=>&$val){
                    $val['code'] = explode(',',$val['item']);
                }
            }
            return $itemArr;
        }
    }

    public function setItemAttr($value,$data){
        if(empty($value)){
            return '';
        }
        if(is_array($value)){
            if($data['cate'] == 1){
                foreach($value as $key=>$val){
                    $itemArr[$key]['item'] = str_replace(['，',','],',',$val);
                }
            }else{
                $code = ['A','B','C','D','E','F'];
                foreach($value as $key=>$val){
                    $itemArr[$key] = [
                        'code'=>$code[$key],
                        'item'=>$val
                    ];
                }
            }
            return json_encode($itemArr);
        }
    }
    public function fruit($data){

    }
}